Data = {}

function DataInitialize()
	sql.Begin()
	sql.Query("CREATE TABLE IF NOT EXISTS ze_ar('unique_id' TEXT NOT NULL, 'exp' INTEGER NOT NULL, PRIMARY KEY('unique_id'));")
	sql.Query("CREATE TABLE IF NOT EXISTS ze_smg('unique_id' TEXT NOT NULL, 'exp' INTEGER NOT NULL, PRIMARY KEY('unique_id'));")
	sql.Query("CREATE TABLE IF NOT EXISTS ze_sg('unique_id' TEXT NOT NULL, 'exp' INTEGER NOT NULL, PRIMARY KEY('unique_id'));")
	sql.Query("CREATE TABLE IF NOT EXISTS ze_pist('unique_id' TEXT NOT NULL, 'exp' INTEGER NOT NULL, PRIMARY KEY('unique_id'));")
	sql.Commit()
	
	MsgN("Data initiated.")
end

function Data.StoreExp(ply, class, amount)
	if not IsValid(ply) then return end
	
	local unique_id = sql.SQLStr(ply:GetNWString("SteamID"))
	
	local result = sql.QueryValue("SELECT exp FROM ze_"..class.." WHERE unique_id = "..unique_id..";")
	if result then
		sql.Query("UPDATE ze_"..class.." SET exp = "..amount.." WHERE unique_id = "..unique_id..";")
	else
		sql.Query("INSERT INTO ze_"..class.." VALUES ("..unique_id..", "..amount..");")
	end
	
	ply:SetNWInt(class.."_exp", amount)
end

function Data.RetrieveExp(ply, class)
	if not IsValid(ply) then return 0 end
	
	local unique_id = sql.SQLStr(ply:GetNWString("SteamID"))
		
	local result = sql.QueryValue("SELECT exp FROM ze_"..class.." WHERE unique_id = "..unique_id..";")
	if result then
		ply:SetNWInt(class.."_exp", result)
		return result
	else
		Data.StoreExp(ply, class, 0)
		return 0
	end
end